package tour0406.demo010402; /***
 * Excerpted from "The Definitive ANTLR 4 Reference",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/tpantlr2 for more book information.
 ***/

import antlr4demo.tour0406.demo010402.LabeledExprLexer;
import antlr4demo.tour0406.demo010402.LabeledExprParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.codehaus.plexus.util.StringUtils;

/*
 * 访问器：构建计算器
 * */
public class Calc {
    public static void main(String[] args) throws Exception {
        String cm = "1+2 \n";
        String cmText = StringUtils.trim(cm);
//        CodePointCharStream input = CharStreams.fromString(cmText);
        CodePointCharStream input = CharStreams.fromString(cm);
        LabeledExprLexer lexer = new LabeledExprLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        LabeledExprParser parser = new LabeledExprParser(tokens);
        ParseTree tree = parser.prog(); // parse

        EvalVisitor eval = new EvalVisitor();
        eval.visit(tree);
    }
}
